global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"

capture log close
log using "$log/16_unbalanced_distribution", replace


*Loop over 37 months
cap program drop loop
program define loop 
args year month ref end treat event
clear all 

	*Define base month 
		local m=mod(`month'-1+11,12)+1
	    local y=`year'+int((`month'-1+11)/12)
		capture use "$temp\cleaned_`y'`m'", clear
		capture use "$temp\cleaned_`y'0`m'", clear
		
		*Keep only balanced firms (but workers can vary over time)
		merge m:1 client_id using "$output\balanced_firms_`ref'.dta"
		keep if _merge==3
		
		*Keep main variables 
		rename salaried salaried0
		rename base base0
		keep state client_id emp_pur_c salaried0 base0
		
		tempfile base_stayers
		save `base_stayers'
	
	*Loop over each month, keeping only stayers 
	forvalues i=0/36 {
	    local m=mod(`month'-1+`i',12)+1
	    local y=`year'+int((`month'-1+`i')/12)
		
		*Import
		capture use "$temp\cleaned_`y'`m'", clear
		capture use "$temp\cleaned_`y'0`m'", clear
				
		*Save for worker panel 
		preserve
			*Keep incumbents
			merge m:1 state client_id emp_pur_c using `base_stayers'
			keep if _merge==3
			drop _merge
			compress
			save "$temp\workers_`i'.dta", replace
		restore 
		
		*Collapse for firm panel
		gen bin=min(floor((base-913)/40)*40+913, 2513)
		collapse (count) n=base (sum) base, by(yr_month client_id salaried bin)
		
		*Expand to fill in bin
			gegen group=group(client_id salaried)
			replace bin=(bin-913)/40
			tsset group bin  
			tsfill, full
				
			bysort group: carryforward yr_month, replace
			bysort group: carryforward client_id, replace
			bysort group: carryforward salaried, replace
			
			gsort group -bin 
			bysort group: carryforward yr_month, replace
			bysort group: carryforward client_id, replace
			bysort group: carryforward salaried, replace
				
			drop group
			replace bin=bin*40+913
			
		*Expand to fill in salaried
			gegen group=group(client_id bin)
			tsset group salaried 
			tsfill, full
				
			bysort group: carryforward yr_month, replace
			bysort group: carryforward client_id, replace
			bysort group: carryforward bin, replace
				
			gsort group -salaried 
			bysort group: carryforward yr_month, replace
			bysort group: carryforward client_id, replace
			bysort group: carryforward bin, replace
				
			drop group
			
		*Fill in missing outcomes 
			replace n=0 if n==.
			replace base=0 if base==.
			
		*Save 
			compress
			save "$temp\workers_bin_`i'", replace
	}

	*Append worker level
	clear all 
	forvalues i=0/36 {
		local m=mod(`month'-1+`i',12)+1
	    local y=`year'+int((`month'-1+`i')/12)
	    append using "$temp\workers_`i'.dta"
	}
	save "$output\unbalanced_panel_`ref'.dta", replace
	
	*Append distribution
	clear all 
	forvalues i=0/36 {
		local m=mod(`month'-1+`i',12)+1
	    local y=`year'+int((`month'-1+`i')/12)
	    append using "$temp\workers_bin_`i'.dta"
	}
	save "$output\unbalanced_distribution_`ref'.dta", replace
end

loop 2015 5 treat 
loop 2013 5 control 

*Delete temp files
forvalues i=0/36 {
	erase "$temp\workers_`i'.dta"
	erase "$temp\workers_bin_`i'.dta"
}

log close
